package com.levy.utils.FileTool;

import com.levy.utils.StringUtils1;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.Scanner;

/**
 * Created by 郭利伟 on 2017.08.22.
 */
public class TextFileUtils {
    // 读取指定路径文本文件
    public static String read(String filePath) {
        StringBuilder str = new StringBuilder();
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(filePath));
            String s;
            try {
                while ((s = in.readLine()) != null)
                    str.append(s + '\n');
            } finally {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str.toString();
    }

    // 写入指定的文本文件，append为true表示追加，false表示重头开始写，
    //text是要写入的文本字符串，text为null时直接返回
    public static void write(String filePath, boolean append, String content) {
        if (content == null)
            return;
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(filePath,
                    append));
            try {
                out.write(content);
            } finally {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write(String path, String content, String encoding)
            throws IOException {
        File file = new File(path);
        //如果目录不存在则新建
        File fileParent = file.getParentFile();
        if(!fileParent.exists()){
            fileParent.mkdirs();
        }
        file.createNewFile();

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(file), encoding));
        writer.write(content);
        writer.close();
    }

    public static String read(String path, String encoding) throws IOException {
        String content = "";
        File file = new File(path);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(file), encoding));
        String line = null;
        while ((line = reader.readLine()) != null) {
            content += line + "\n";
        }
        reader.close();
        return content;
    }

    public static String readByScanner(String path,String encoding){
        StringBuilder sb = new StringBuilder();
        Scanner sc;
        try {
            InputStream is = new FileInputStream(new File(path));
            sc = new Scanner(is, encoding);
            while (sc.hasNextLine()) {
                sb.append(sc.nextLine()).append("\n");
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return sb.toString();
    }



    public static byte[] readFile(String filename) throws IOException {
        if (filename == null || filename.equals("")) {
            throw new IOException("无效的文件路径");
        }
        File file = new File(filename);
        long len = file.length();
        byte[] bytes = new byte[(int) len];
        BufferedInputStream bufferedInputStream = new BufferedInputStream(
                new FileInputStream(file));
        int r = bufferedInputStream.read(bytes);
        if (r != len)
            throw new IOException("读取文件不正确");
        bufferedInputStream.close();
        return bytes;
    }


    public static void copyFile(String fromFile,String toFile) throws IOException{
        FileInputStream ins = new FileInputStream(new File(fromFile));
        FileOutputStream out = new FileOutputStream(new File(toFile));
        byte[] b = new byte[1024];
        int n=0;
        while((n=ins.read(b))!=-1){
            out.write(b, 0, n);
        }

        ins.close();
        out.close();
    }


    public static void main(String[] args) throws Exception {

//        String read = read("D:\\Idea_workspace\\levy\\src\\main\\resources\\xstreamtest.xml");
//        System.out.println(read);
//        StringBuilder sb = new StringBuilder();
//        Scanner sc;
//        try {
//            String path = "C:\\Users\\liwei\\Desktop\\to_liwei\\to_liwei\\tm_trvl_usr_draw_d.txt";
//            InputStream is = new FileInputStream(new File(path));
//            sc = new Scanner(is, "UTF-8");
//            while (sc.hasNextLine()) {
//                String s = sc.nextLine();
//                if (StringUtils1.isNotBlank(s)){
//                    if (!s.contains("番禺长隆")){
//                        sb.append(s).append("\r\n");
//                    }
//                }
//            }
//            System.out.println(sb.toString());
//            write("C:\\Users\\liwei\\Desktop\\to_liwei\\to_liwei\\tm_trvl_usr_draw_d2.txt",false,sb.toString());


//
//        }catch (Exception e){
//            e.printStackTrace();
//        }


    }
}
